/*
作用：一些文件节点操作函数
作者：孙泽宇
属于mvc中的c层
*/

#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<dir.h>
#include<string.h>
#include<math.h>
#include"filestruct.h"
#include"svga.h"
#include"draw.h"
#include"mouse.h"
#include"putword.h"
#include"draw_item.h"
#include"input.h"
#include"disk.h"
#include"file.h"
#define N 50 //文件路径字符串大小

/*
作用：计算levenshtein距离(大小写不敏感)
输入：两字符串
输出：无
返回值：距离
*/
char edit_distance(char *a,char *b)
{
	char d[13][13];
	/*
	d[i][j]表示表示A串从第0个字符开始到第i个字符和B串从第0个
	字符开始到第j个字符，这两个字串的编辑距离。
	*/
	char i = 0;//循环辅助变量
	char j = 0;//循环辅助变量
	char flag = 0;//标记
	for(i = 0; i < 13; i++)		//初始化
	{
		for(j = 0; j < 13; j++)
		{
			d[i][j] = 0;
		}
	}
	for(i = 0; i < strlen(a); i++)
	{
        d[i][0] = i;
	}
    for(j = 0; j < strlen(b); j++)
	{
        d[0][j] = j;
	}
    for(i = 1; i < strlen(a); i++)
    {
        for(j = 1; j < strlen(b); j++)
        {
            if((a[i] == b[j]) || ((a[i] - b[j]) == 32) || ((b[j] - a[i]) == 32))
			{
                flag = 0;
			}
            else
			{
                flag = 1;
			}
            d[i][j] = min(d[i-1][j]+1,min(d[i][j-1]+1,d[i-1][j-1]+flag));
            /*d[i-1][j]+1 delete a last a[i]
            d[i][j-1]+1 add b last str
            d[i-1][j-1]+flag means change,same dont neet chance,
			diffrent need chance,flag*/
        }
    }
    return d[strlen(a)-1][strlen(b)-1];
}

/*
作用：计算KMP中模式串对应next数组
输入：模式串pat，next数组
输出：无
返回值：无
*/
void make_next(char *pat,char *next)
{
    int i = 2;
    int j = 1;
	next[1] = 0;
    next[2] = 1;
    while(i < strlen(pat)) 
	{
        if((j == 0) || (pat[i-1] == pat[j-1])) 
		{
            i++;
            j++;
            if(pat[i-1] != pat[j-1])
			{
               next[i] = j;
            }
            else
			{
                next[i] = next[j];
            }
        }
		else
		{
            j = next[j];
        }
    }
	return;
}

/*
作用：快速模式匹配(KMP算法)(大小写不敏感)
输入：两字符串
输出：无
返回值：-1为未匹配上，0为匹配上
*/
char pat_match(char *pat,char *txt)
{
	char next[14];
    int i = 1;
    int j = 1;
	make_next(pat,next);
    while((i <= strlen(txt)) && (j <= strlen(pat)))
	{
        if((j == 0) || (txt[i-1] == pat[j-1]) || (txt[i - 1] == (pat[i - 1] + 32)) || (txt[i - 1] == (pat[i - 1] - 32)))
		{
            i++;
            j++;
        }
        else
		{
            j = next[j];
        }
    }
    if (j > strlen(pat))
	{
        return 0;
    }
    return -1;
}


/*
作用：搜索(模糊搜索)
输入：查找的字符串,根
输出：无
返回值：无
*/
void node_search_1(char *s,file_node *root)
{
	char distance = 0;	//记录距离
	int head = 0;	//记录队首位置
	int tail = 0;	//记录队尾位置
	char size[5];	//储存size
	char path[N];	//储存path
	file_node **a;	//队列
	file_node *p;	//辅助指针
	FILE *fp;
	char i = 0;
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	chdir("c:\\hanziku\\");
	fp = fopen("searcht.txt","wt+");
	if(fp == NULL)
	{
		printf("node_search_1");
		exit(1);
	}
	a = (file_node **)malloc(100*sizeof(file_node *));
	if(a == NULL)
	{
		printf("memory");
		exit(1);
	}
	a[tail++] = root;
	while(head < tail)
	{
		p = a[head++];
		distance = edit_distance(s,p->file.name);
		if((strlen(p->file.name) >= (4 * distance)) || (pat_match(s,p->file.name) == 0))
		{
			myfputs(p->file.name,13,fp);
			longtochar(p->file.size,size);
			myfputs(size,5,fp);
			search_nodepath(p,path);
			myfputs(path,N,fp);
		}
		if(p->child != NULL)
		{
			a[tail++] = p->child;
		}
		if(p->next != NULL)
		{
			a[tail++] = p->next;
		}
	}
	fclose(fp);
	free(a);
	return;
}

/*
作用：精确搜索
输入：匹配的两个字符串，测试的字符串可用通配符
输出：无
返回值：匹配返回1，不匹配返回0
*/
int str_match(char *s1,char *s2)	//s1中可含正则表达式中几个特殊字符
{
	char i = 0;//循环辅助变量
	char j = 0;//循环辅助变量
	char *p = NULL;//循环辅助
	p = strstr(s1,"*");
	if(p == NULL)//如果没有*通配符
	{
		while(s1[i] != 0)
		{
			if(s1[i] != s2[i])
			{
				if(s1[i] != '?')
				{
					return 0;
				}
			}
			i++;
		}
		return 1;
	}
	/*
	一下为有*时的情况
	*/
	while(1)//定位*
	{
		if(s1[i] == *p)
		{
			break;
		}
		i++;
	}
	while(j < i)//匹配*之前的字符
	{
		if(s1[j] != s2[j])
		{
			if(s1[j] != '?')
			{
				return 0;
			}
		}
		j++;
	}
	j = i;
	i++;
	if(s1[i] == NULL)
	{
		return 1;
	}
	while((s1[i] != NULL) && (s2[j] != NULL))
	{	
		while(s2[j] != s1[i])
		{
			j++;
			if(s2[j] == NULL)
			{
				return 0;
			}
		}
		i++;
		j++;
	}
	if(s2[j] == NULL)
	{
		return 0;
	}
	if(s1[i] == NULL)
	{
		return 1;
	}
	return 0;
}

/*
作用：搜索文件(精确搜索)(dos下全为大写，大小写不敏感)
输入:查找的字符串(可使用通配符),根
输出：无
返回值：对应搜索出的链表头
*/
/*
？表示某个字符
*表示任意个任意字符
*/
void node_search_2(char *s,file_node *root)
{
	int head = 0;	//记录队首位置
	int tail = 0;	//记录队尾位置
	char size[5];	//储存size
	char path[N];	//储存path
	file_node **a;	//队列
	file_node *p;	//辅助指针
	FILE *fp;
	char i = 0;
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	chdir("c:\\hanziku\\");
	fp = fopen("searcht.txt","wt+");
	if(fp == NULL)
	{
		printf("node_search_1");
		exit(1);
	}
	a = (file_node **)malloc(100*sizeof(file_node *));
	if(a == NULL)
	{
		printf("memory");
		exit(1);
	}
	a[tail++] = root;
	while(head < tail)
	{
		p = a[head++];
		if((p->file.name[0] == s[0]) || (s[0] == '*'))
		{
			if(str_match(s,p->file.name) == 1)
			{
				myfputs(p->file.name,13,fp);
				longtochar(p->file.size,size);
				myfputs(size,5,fp);
				search_nodepath(p,path);
				myfputs(path,N,fp);
			}
		}
		if(p->child != NULL)
		{
			a[tail++] = p->child;
		}
		if(p->next != NULL)
		{
			a[tail++] = p->next;
		}
	}
	fclose(fp);
	free(a);
	return;
}

/*
作用：附加层级的搜索(不包括0级)
输入：查找的字符串及文件所在层级,根
输出：无
返回值：无
*/
void node_search_3(char *s,char n,file_node *root)
{
	char distance;	//记录距离
	int head = 0;	//记录队首位置
	int tail = 0;	//记录队尾位置
	char size[5];	//储存size
	char path[N];	//储存path
	file_node **a;	//队列
	file_node *p;	//辅助指针
	char i = 0;
	FILE *fp;
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	chdir("c:\\hanziku\\");
	fp = fopen("searcht.txt","wt+");
	if(fp == NULL)
	{
		printf("node_search_1");
		exit(1);
	}
	a = (file_node **)malloc(100*sizeof(file_node *));
	if(a == NULL)
	{
		printf("memory");
		exit(1);
	}
	a[tail++] = root;
	while(head < tail)
	{
		p = a[head++];
		distance = edit_distance(s,p->file.name);
		if(((p->file.time & n) == n) && ((strlen(p->file.name) >= (4 * distance)) || (pat_match(s,p->file.name) == 0)))
		{
			myfputs(p->file.name,13,fp);
			longtochar(p->file.size,size);
			myfputs(size,5,fp);
			search_nodepath(p,path);
			myfputs(path,N,fp);
		}
		if(p->child != NULL)
		{
			a[tail++] = p->child;
		}
		if(p->next != NULL)
		{
			a[tail++] = p->next;
		}
	}
	fclose(fp);
	free(a);
	return;
}

/*
作用：查找到最后一个文件夹(存在文件)
输入：子链表头
输出：无
返回值：最后一个文件夹节点(若不存在文件则返回NULL)
*/
file_node *find_lastdir(file_node *head)
{
	file_node *p;
	p = head;
	if((p->file.type & 1) == 1)
	{
		return NULL;
	}
	while((p->next != NULL) && ((p->next->file.type & 2) == 2))
	{
		p = p->next;
	}
	if(p->next == NULL)
	{
		return NULL;
	}
	if((p->next->file.type & 1) == 1)
	{
		return p;
	}
	return NULL;
}

/*
作用：查找子链表中有多少节点
输入：链表头
输出：无
返回值：n
*/
char find_nodenum(file_node *head)
{
	char n = 0;
	file_node *p;
	p = head;
	while(p != NULL)
	{
		p = p->next;
		n++;
	}
	return n;
}

/*
作用：查找子链表中有多少个目录
输入：链表头
输出：无
返回值：n
*/
char find_dirnum(file_node *head)
{
	char n = 0;
	file_node *p;
	p = head;
	while((p != NULL) && ((p->file.type & 2) == 2))
	{
		p = p->next;
		n++;
	}
	return n;
}

/*
作用：查找子链表中有多少个文件
输入：链表头
输出：无
返回值：n
*/
char find_filenum(file_node *head)
{
	char n = 0;
	file_node *p;
	p = find_lastdir(head);
	if(p == NULL)
	{
		p = head;
	}
	else
	{
		p = p->next;
	}
	while((p != NULL) && ((p->file.type & 1) == 1))
	{
		p = p->next;
		n++;
	}
	return n;
}

/*
作用：时间比较
输入：两文件节点
输出：无
返回值：第一个时间较近返回1，第二个较近返回-1，相等返回0
*/
char time_cmp(file_node *node1,file_node *node2)
{
	if(node1->file.date > node2->file.date)
	{
		return 1;
	}
	else if(node1->file.date < node2->file.date)
	{
		return -1;
	}
	else
	{
		if((node1->file.time & 65504) > (node2->file.time & 65504))
		{
			return 1;
		}
		else if((node1->file.time & 65504) < (node2->file.time & 65504))
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
}

/*
作用：文件排序(修改时间)
输入：链表头，递增1/递减0
输出：无
返回值：无
*/
void node_sort_time(file_node *head,char mode)//使用冒泡排序
{
	char i = 0;//循环辅助变量
	char j = 0;//循环辅助变量
	file_node *p;
	file_node *q;
	file_node *temp;
	p = head;
	temp = find_lastdir(head);
	if(temp == NULL)
	{
		if(mode == 1)
		{
			i = find_nodenum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) > 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
		if(mode == 0)
		{
			i = find_nodenum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) < 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
	}
	else
	{
		if(mode == 1)
		{
			i = find_dirnum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) > 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			i = find_filenum(head);
			for(;i > 1; i--)
			{
				p = temp->next;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) > 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{	
						p = p->next;
					}
				}
			}
			return;
		}
		if(mode == 0)
		{
			i = find_dirnum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) < 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			i = find_filenum(head);
			for(;i > 1; i--)
			{
				p = temp->next;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(time_cmp(p,q) < 0)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
	}
}

/*
作用：文件排序(大小)
输入：链表头，递增1/递减0
输出：无
返回值：无
*/
void node_sort_size(file_node *head,char mode)
{
	char i = 0;
	char j = 0;
	file_node *p;
	file_node *q;
	file_node *temp;
	p = head;
	temp = find_lastdir(head);
	if(temp == NULL)
	{
		if(mode == 1)
		{
			i = find_nodenum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(p->file.size > q->file.size)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
		if(mode == 0)
		{
			i = find_nodenum(head);
			for(;i > 1; i--)
			{
				p = head;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(p->file.size < q->file.size)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
	}
	else
	{
		if(mode == 1)
		{
			i = find_filenum(head);
			for(;i > 1; i--)
			{
				p = temp->next;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(p->file.size > q->file.size)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
		if(mode == 0)
		{
			i = find_filenum(head);
			for(;i > 1; i--)
			{
				p = temp->next;
				for(j = 0; j < i - 1; j++)
				{
					q = p->next;
					if(q == NULL)
					{
						break;
					}
					if(p->file.size < q->file.size)
					{
						if((p->file.type & 32) == 32)
						{
							p->pre->child = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
							p->file.type = p->file.type ^ 32;
							q->file.type = q->file.type | 32;
						}
						else if((p->file.type & 32) != 32)
						{
							p->pre->next = q;
							q->pre = p->pre;
							if(q->next != NULL)
							{
								p->next = q->next;
								q->next->pre = p;
							}
							else
							{
								p->next = NULL;
							}
							q->next = p;
							p->pre = q;
						}
					}
					else
					{
						p = p->next;
					}
				}
			}
			return;
		}
	}
}

/*
作用：文件排序(名称)
输入：链表头
输出：无
返回值：无
*/
void node_sort_name(file_node *head)
{
	char i = 0;
	char j = 0;
	file_node *p;
	file_node *q;
	file_node *temp;
	p = head;
	temp = find_lastdir(head);
	if(temp == NULL)
	{
		i = find_nodenum(head);
		for(;i > 1; i--)
		{
			p = head;
			for(j = 0; j < i - 1; j++)
			{
				q = p->next;
				if(q == NULL)
				{
					break;
				}
				if(strcmp(p,q) > 0)
				{
					if((p->file.type & 32) == 32)
					{
						p->pre->child = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}	
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
						p->file.type = p->file.type ^ 32;
						q->file.type = q->file.type | 32;
					}
					else if((p->file.type & 32) != 32)
					{
						p->pre->next = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
					}
				}
				else
				{
					p = p->next;
				}
			}
		}
	}
	else
	{
		i = find_dirnum(head);
		for(;i > 1; i--)
		{
			p = head;
			for(j = 0; j < i - 1; j++)
			{
				q = p->next;
				if(q == NULL)
				{
					break;
				}
				if(strcmp(p,q) > 0)
				{
					if((p->file.type & 32) == 32)
					{
						p->pre->child = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
						p->file.type = p->file.type ^ 32;
						q->file.type = q->file.type | 32;
					}
					else if((p->file.type & 32) != 32)
					{
						p->pre->next = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
					}
				}
				else
				{
					p = p->next;
				}
			}
		}
		i = find_filenum(head);
		for(;i > 1; i--)
		{
			p = temp->next;
			for(j = 0; j < i - 1; j++)
			{
				q = p->next;
				if(q == NULL)
				{
					break;
				}
				if(strcmp(p,q) > 0)
				{
					if((p->file.type & 32) == 32)
					{
						p->pre->child = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
						p->file.type = p->file.type ^ 32;
						q->file.type = q->file.type | 32;
					}
					else if((p->file.type & 32) != 32)
					{
						p->pre->next = q;
						q->pre = p->pre;
						if(q->next != NULL)
						{
							p->next = q->next;
							q->next->pre = p;
						}
						else
						{
							p->next = NULL;
						}
						q->next = p;
						p->pre = q;
					}
				}
				else
				{
					p = p->next;
				}
			}
		}
		return;
	}
}
/*
设置为隐含：h0
设置为系统：s0
取消隐含：h1
取消系统：s1
*/
/*
作用：将文件属性设置为隐含文件
输入：文件节点
输出：无
返回值：无
*/
void change_type_hidden(file_node *node)
{
	char size[5];
	char path[N];
	char i = 0;//循环辅助变量
	FILE *fp;
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	i = 0;
	chdir("c:\\hanziku\\");
	fp = fopen("type.txt","at+");
	if(fp == NULL)
	{
		printf("change_type_hidden");
		exit(1);
	}
	longtochar(node->file.size,size);
	search_nodepath(node,path);
	myfputs(node->file.name,13,fp);
	myfputs(size,5,fp);
	myfputs(path,N,fp);
	myfputs("h0",3,fp);//储存更改类型
	node->file.type = node->file.type | 4;
	fclose(fp);
}

/*
作用：将文件属性设置为系统文件
输入：文件节点
输出：无
返回值：无
*/
void change_type_system(file_node *node)
{
	char size[5];
	char path[N];
	char i = 0;//循环辅助变量
	FILE *fp;
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	i = 0;
	chdir("c:\\hanziku\\");
	fp = fopen("type.txt","at+");
	if(fp == NULL)
	{
		printf("change_type_system");
		exit(1);
	}
	longtochar(node->file.size,size);
	search_nodepath(node,path);
	myfputs(node->file.name,13,fp);
	myfputs(size,5,fp);
	myfputs(path,N,fp);
	myfputs("s0",3,fp);//储存更改类型
	node->file.type = node->file.type | 16;
	fclose(fp);
}

/*
作用：取消隐含文件属性
输入：文件节点
输出：无
返回值：无
*/
void change_type_reh(file_node *node)
{
	char size[5];
	char path[N];
	char i = 0;//循环辅助变量
	FILE *fp;
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	i = 0;
	chdir("c:\\hanziku\\");
	fp = fopen("type.txt","at+");
	if(fp == NULL)
	{
		printf("change_type_reh");
		exit(1);
	}
	longtochar(node->file.size,size);
	search_nodepath(node,path);
	myfputs(node->file.name,13,fp);
	myfputs(size,5,fp);
	myfputs(path,N,fp);
	myfputs("h1",3,fp);//储存更改类型
	node->file.type = node->file.type ^ 4;
	fclose(fp);
}

/*
作用：取消系统文件属性
输入：文件节点
输出：无
返回值：无
*/
void change_type_res(file_node *node)
{
	char size[5];
	char path[N];
	char i = 0;//循环辅助变量
	FILE *fp;
	for(i = 0; i < 5; i++)
	{
		size[i] = '\0';
	}
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
	i = 0;
	chdir("c:\\hanziku\\");
	fp = fopen("type.txt","at+");
	if(fp == NULL)
	{
		printf("change_type_res");
		exit(1);
	}
	longtochar(node->file.size,size);
	search_nodepath(node,path);
	myfputs(node->file.name,13,fp);
	myfputs(size,5,fp);
	myfputs(path,N,fp);
	myfputs("s1",3,fp);//储存更改类型
	node->file.type = node->file.type ^ 16;
	fclose(fp);
}
/*
作用：运行exe文件
输入：文件节点
输出：无
返回值：无
*/
void run_exe(file_node *node)
{
	char temp[N+24];     //辅助字符串
	char i = 0;
	for(i = 0; i < N+24; i++)
	{
		temp[i] = '\0';
	}
    search_nodepath(node,temp);
	system(temp);   //系统调用，借用编辑器打开文本文件
	delay(1000);  //短暂停留
}
/*
作用：调用bc编辑文件
输入：文件节点
输出：无
返回值：无
*/
void run_bc(file_node *node)
{
	char temp[23] = "C:\\BORLANDC\\BIN\\BC.EXE ";   //辅助字符串,调用bc编辑器
	char path[N];
	char i = 0;
	for(i = 0; i < N; i++)
	{
		path[i] = '\0';
	}
    search_nodepath(node,path);
	strcat(temp,path);	
	system(temp);//系统调用，借用BC编辑器打开文本文件
	delay(1000);
	chdir(path);
	change_time(node);
	change_size(node);
}